package vip.liux.front.domain.models.chat;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Getter;
import lombok.Setter;
import vip.liux.domain.entities.auditing.CreationAuditedAggregateRoot;

import java.time.Instant;

/**
 * 聊天消息
 */
@Getter
@Setter
@Entity
@Table(name = "chat_messages")
@org.springframework.data.relational.core.mapping.Table("chat_messages")
public class ChatMessage extends CreationAuditedAggregateRoot<Long, ChatMessage> {

    /**
     * 租户ID
     */
    @Column(name = "tenant_id")
    private String tenantId;

    /**
     * 消息内容
     */
    @Size(max = 4096)
    @NotNull
    @Column(name = "text", nullable = false, length = 4096)
    private String text;

    /**
     * 是否全部已读
     */
    @NotNull
    @Column(name = "all_read", nullable = false)
    private boolean allRead = false;

    /**
     * 已读时间
     */
    @Column(name = "read_time")
    private Instant readTime;

    public ChatMessage(String text, String tenantId) {
        this.text = text;
        this.tenantId = tenantId;
    }

    public ChatMessage() {

    }

    /**
     * 标记为已读
     *
     * @param now 当前时间
     */
    public void markAsAllRead(Instant now) {
        this.allRead = true;
        this.readTime = now;
    }
}